package com.knowswift.issues.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.knowswift.common.bean.issue.po.Authorize;
import com.knowswift.common.bean.issue.po.Customer;
import com.knowswift.common.bean.issue.vo.ModuleDto;
import com.knowswift.common.bean.project.po.Project;
import com.knowswift.issues.mapper.CustomerMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author CTL
 * @create 2025-07-02 16:30
 */

@Service
public class CustomerService extends ServiceImpl<CustomerMapper, Customer> {
    @Resource
    private AuthorizeService authorizeService;
    public void ProjectAuthorize(String customerId, List<ModuleDto> moduleList) {
        //删除原有客户授权项目
        authorizeService.getBaseMapper().deleteTermiteAuthorizeByCustomerId(customerId);
        //保存新的客户授权项目
        List<Authorize> authorizeEntityList = new ArrayList<>();
        for (ModuleDto module : moduleList) {
            for (Project project : module.getProjects()) {
                Authorize authorize = new Authorize();
                authorize.setCustomerId(customerId);
                authorize.setModuleId(module.getModuleId());
                authorize.setModuleName(module.getModuleName());
                authorize.setModuleOrder(module.getModuleOrder());
                authorize.setProjectName(project.getProjectName());
                authorize.setProjectOrder(project.getProjectOrder());
                authorizeEntityList.add(authorize);
            }
        }
        authorizeService.saveBatch(authorizeEntityList);
    }

    public void saveCustomer(Customer customer) {
        String maxCustomerCode = baseMapper.getMaxCustomerCode();
        String customerCode = String.format("%04d", Integer.parseInt(maxCustomerCode) + 1);
        customer.setCustomerCode(customerCode);
        customer.setCreateTime(new Date());
        this.save(customer);
    }
}
